package monster.comm.interceptor;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;

import monster.comm.log.ILog;
import monster.comm.log.LogService;
import monster.comm.util.SysConstant;
/**
 * 
 * @author 蒋增奎
 *  struts2登录拦截器
 */
public class LoginIterceptor extends AbstractInterceptor{
  private static ILog log = LogService.getLog(LoginIterceptor.class);
  public String intercept(ActionInvocation action)throws Exception{
	  String ret="";
	  ActionContext ctx = action.getInvocationContext();
	  HttpServletRequest request=ServletActionContext.getRequest();   
	  HttpSession session=request.getSession();
	  log.info(ctx.getName());
	 //ctx.getName()得到调用拦截器的action的action名称

	  // 取出名为user的session属性
	  //User user = (User) session.get("user");
	//获得request
	  

      Object obj=session.getAttribute(SysConstant.SESSION_NAME);
      if(obj==null && !"login".equals(ctx.getName())){//没有登录
    	log.error("登录失效，等重新登录");
    	setToGoingURL(request,action);
    	return "login";
      }
	   ret=action.invoke();

	  return ret;
  }
  
  private void setToGoingURL(HttpServletRequest request,ActionInvocation invocation)   
  {   
      //如果referer不为空 直接使用它。如果为空我们分别获得命名空间，action名,以及请求参数   
      //从新构造成一个URL保存在session中   
      String url=request.getHeader("referer");   
      log.info("待转向URL:"+request.getHeader("referer"));   
      if(url==null || url.equals(""))   
          {   
              url="";   
              String path=request.getContextPath();   
              String actionName=invocation.getProxy().getActionName();   
              String nameSpace=invocation.getProxy().getNamespace();   
              if(nameSpace!=null && nameSpace.trim().length()!=0)   
                  {   
                      url = url+path+nameSpace;   
                         
                  }   
              if(actionName!=null && actionName.trim().length()!=0)   
                  {   
                      url = url+"/"+actionName+".action";   
                  }   
                 
              Map<String,String[]> zzMap = request.getParameterMap(); 
              String params="";
              if(zzMap!=null)   
                  {   
                      for(String s:zzMap.keySet())   
                          {   
                              String[] value=zzMap.get(s);   
                              for(String val:value)   
                                  {   
                            	  params=params+s+"="+val+"&";   
                                  }   
                                 
                          }   
                  }   
              if(!"".equals(params)){
            	  params=params.substring(0,params.length()-1);
            	  url=url+"?"+params;
              }
              log.info("完整URL:"+url);   
          }   
      request.setAttribute("xt_go_back_url", url);   
  }
  
  
}

